package com.nengchuan.eic.nceconomy.controller;

import com.nengchuan.eic.nceconomy.component.template.BaseHandler;
import com.nengchuan.eic.nceconomy.component.template.RequestModel;
import com.nengchuan.eic.nceconomy.component.template.ResponseModel;
import com.nengchuan.eic.nceconomy.config.BaseConfig;
import com.nengchuan.eic.nceconomy.entity.po.TemplateFile;
import com.nengchuan.eic.nceconomy.entity.vo.ExcelSheetListVO;
import com.nengchuan.eic.nceconomy.entity.vo.SheetDetail;
import com.nengchuan.eic.nceconomy.service.OssService;
import com.nengchuan.eic.nceconomy.service.TemplateDicService;
import com.nengchuan.eic.nceconomy.util.PoiUtils;
import com.nengchuan.model.model.WrappedResult;
import com.nengchuan.util.NullCheckUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@Api(value = "TemplateController", tags = "模板功能")
public class TemplateController {

    @Autowired
    private BaseHandler baseTemplateHandler;

    @Autowired
    private TemplateDicService templateDicService;

    @Autowired
    private OssService ossService;

    @Autowired
    private BaseConfig baseConfig;


    @PostMapping(value = "/upload-template",name = "模板上传API接口")
    public WrappedResult upload(@RequestParam("f") MultipartFile file,
                                @RequestParam("cc") String categoryCode,
                                @RequestParam("pc") String projectCode){
        if(NullCheckUtils.isNull(file)||NullCheckUtils.isNull(categoryCode)|| NullCheckUtils.isNull(projectCode)){
            return WrappedResult.fail("参数不能为空");
        }
        try {
            Map<String,List<String[]>> datas=PoiUtils.readExcel(file);
            log.info("#upload# 正在进行能源局预规上传,excel解析完毕;[sheet数量:{},categoryCode:{},projectCode:{}]",datas.size(),categoryCode,projectCode);

            RequestModel request=new RequestModel();
            request.setData(datas);
            request.setFileName(file.getOriginalFilename());
            request.setCategoryCode(categoryCode);
            request.setProjectCode(projectCode);

            List<ResponseModel> responses=baseTemplateHandler.handlerRequest(request);
            log.info("#upload# 能源局预规excel转对象完毕;[对象数量:{}]",responses.size());

            Long ossId=1L;
            if(!"dev".equals(baseConfig.getSpringProfileActive())) {
                ossId = ossService.uploadByOss(file);
            }

            TemplateFile templateFile=TemplateFile
                    .builder()
                    .projectIndex(projectCode)
                    .categoryIndex(categoryCode)
                    .ossId(ossId)
                    .excelName(file.getOriginalFilename())
                    .delFlag(0)
                    .build();
            templateDicService.save(responses,templateFile);
        } catch (IOException e) {
            log.error("#upload# 文件解析失败",e);
            return WrappedResult.fail("文件解析失败");
        }
        return WrappedResult.success();
    }

    @ApiOperation("获取模板sheet详情")
    @GetMapping(value = "/getTemplate",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiResponse(code = 200, message = "成功为true,失败为false", response = WrappedResult.class)
    public WrappedResult<SheetDetail> getTemplate(@RequestParam("categoryIndex") String categoryIndex,@RequestParam("sheetId")Long sheetId, @RequestParam("projectIndex") String projectIndex){
        if(NullCheckUtils.isNull(categoryIndex)||NullCheckUtils.isNull(projectIndex)){
            return WrappedResult.fail("参数不能为空");
        }
        return WrappedResult.success(templateDicService.getTemplate(categoryIndex,projectIndex,sheetId));
    }
    @ApiOperation("获取模板sheet列表")
    @GetMapping(value = "/template_sheet_list",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiResponse(code = 200, message = "成功为true,失败为false", response = WrappedResult.class)
    public WrappedResult<ExcelSheetListVO> getTemplateSheetList(@RequestParam("categoryIndex") String categoryIndex, @RequestParam("projectIndex") String projectIndex){
        if(NullCheckUtils.isNull(categoryIndex)||NullCheckUtils.isNull(projectIndex)){
            return WrappedResult.fail("参数不能为空");
        }
        return WrappedResult.success(templateDicService.getTemplateSheetList(categoryIndex,projectIndex));
    }

    @ApiOperation("根据父id查询子内容")
    @GetMapping(value = "/sheet_list_by_pid", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiResponse(code = 200, message = "成功为true,失败为false", response = WrappedResult.class)
    public WrappedResult<List<Map<String,Object>>> getSheetListByPid(@RequestParam("id") Long id,@RequestParam("sheetId") Long sheetId) {
        return WrappedResult.success(templateDicService.getSheetListByPid(id,sheetId));
    }

    @ApiOperation("模板下载")
    @PostMapping(value = "/download", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiResponse(code = 200, message = "成功为true,失败为false", response = WrappedResult.class)
    public WrappedResult<String> download(@RequestParam("templateId")Long templateId) throws Exception{
        if(templateId==null){
            return WrappedResult.fail("参数异常");
        }
        String url = templateDicService.download(templateId);
        if(url==null){
            return WrappedResult.fail("文件不存在");
        }
        return WrappedResult.success(url);
    }

}
